package com.aaa.appUtil.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * fileName:TimerConfig
 * description:
 * author:zxh
 * createTime:2020/1/10 9:27
 * version:1.0.0
 */
@Configuration
@EnableAsync //开启异步事件的支持
public class TimerConfig {
    /*
    @value 从配置中读取变量值
    volatile 在java并发编程中常用于保持内存可见性和防止指令重排序
     */
    @Value("${task.corePoolSize}")
    private volatile int corePoolSize;
    @Value("${task.maxPoolSize}")
    private volatile int maxPoolSize;
    @Value("${task.queueCapacity}")
    private volatile int queueCapacity;

    @Bean
    public Executor taskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.initialize();
        return executor;
    }
}
